<template>
  <el-dialog title="分配角色" :visible.sync="visiable" width="500px" :before-close="handleClose">
    <el-form ref="formData" :model="formData" label-width="100px" label-position="right" style="width:400px;" status-icon>
      <el-form-item label="用户名:" prop="userName" >
        <el-input v-model="formData.userName" ></el-input>
      </el-form-item>
      <el-form-item label="角色:" prop="roleName">
        <template>
          <el-checkbox-group v-model="userRoleIds" @change="changeCheckBox">
              <el-checkbox v-for="(role,index) in allRoleIds" :label="role.roleId" :key="index">{{role.roleDescription}}</el-checkbox>
          </el-checkbox-group>
        </template>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" @click="handleRole">确定</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import roleApi from '@/api/role';
export default {
  props:{
    visiable:{  // 弹出窗口
      type:Boolean,
      default:false
    },
    userRoleIds:{
      type: Array,
      default: []
    },
    formData: { //提交表单数据
      type:Object,
      // eslint-disable-next-line vue/require-valid-default-prop
      default:{}
    },
    remoteClose: Function, //用于关闭窗口
  },

  data() {
    return {
      allRoleIds: [],   // 所有的角色
      checkRoleIds: [], // 选择的角色ids
    }
  },

  created() {
    this.checkRoleIds = this.userRoleIds;
    this.fetchRoleData();
  },

  methods:{
      // 关闭窗口
      handleClose(){
        // 将表单清空
        this.$refs['formData'].resetFields();
        // 调用父组件的方法关闭窗口
        this.remoteClose();
      },

      // 分配角色信息
      handleRole() {
        if (this.checkRoleIds.length === 0) {
          this.$notify({
            title: '警告',
            message: '请至少选择一个角色',
            type: 'warning',
            duration: 1500
          });
        } else {
          var data = {
            userId: this.formData.userId,
            roleIds: this.userRoleIds
          };
          roleApi.addUserRole(data).then(resp => {
            if (resp.code === 200) {
              this.$notify({
                title: '成功',
                message: '分配角色成功',
                type: 'success',
                duration: 1500
              });
            } else if (resp.code === 306) {
              this.$notify.error({
                title: '错误',
                message: '您没有操作权限，请联系管理员',
                duration: 1500
              });
            } else {
              this.$notify.error({
                title: '错误',
                message: '系统异常，请稍后再试',
                duration: 1500
              });
            }
            this.handleClose();
          })
        }
      },

      changeCheckBox(val) {
          this.checkRoleIds = val;
      },

      // 查询所有的角色信息
      fetchRoleData() {
        roleApi.getAllRole().then(response => {
          if (response.code === 200) {
            this.allRoleIds = response.data;
          }
        })
      },
  }
}
</script>

<style scoped>

</style>
